package com.yaoandw.crawler;

import java.io.IOException;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import com.yaoandw.givebook.Constants;
import com.yaoandw.lucene.Page;
import com.yaoandw.lucene.PageIndexer;
import com.yaoandw.lucene.TxtPage;
import com.yaoandw.util.Md5Encode;
import com.yaoandw.util.StringUtil;

public class PageIndexTool {
//	public static String indexDir = "D:/temp/study/java/lucene/firstluceneapp/testindexer";
//	public static String indexDir = "D:/temp/study/java/lucene/firstluceneapp/actizforumindexer";
//	public static String indexDir = "D:/temp/study/java/lucene/firstluceneapp/fileindexer";
	
	private static PageIndexer pageIndexer = null;
	private static Set<String> textHashValueSet = Collections.synchronizedSet(new HashSet<String>());
	public static void createIndex(String metaDesc,String title,String plainText,String url,String contentType) {
		if (!StringUtil.isEmpty(plainText) && contentType != null && contentType.startsWith ("text")){
			if(hasIndexd(metaDesc))
				return ;
			try{
				if(pageIndexer == null){
					pageIndexer = new PageIndexer(Constants.indexDir);
				}
				if(pageIndexer != null){
					Page page = new Page(title,plainText,url,new Date());
					pageIndexer.updateIndex(page );
				}
			}catch(Exception e){
				
			}
		}
	}
	public static void createTxtFileIndex(String metaDesc,String title,String plainText,String pageUrl,String contentType,String txtFileUrl) {
		if (!StringUtil.isEmpty(plainText) && contentType != null && contentType.startsWith ("text")){
			if(hasIndexd(metaDesc))
				return ;
			try{
				if(pageIndexer == null){
					pageIndexer = new PageIndexer(Constants.indexDir);
				}
				if(pageIndexer != null){
					TxtPage page = new TxtPage(title,plainText,pageUrl,new Date(),txtFileUrl);
					pageIndexer.updateIndex(page );
				}
			}catch(Exception e){
				
			}
		}
	}
	private static boolean hasIndexd(String metaDesc) {
		if(StringUtil.isEmpty(metaDesc))
			return false;
		String hashValue = Md5Encode.encode(metaDesc);
		if(textHashValueSet.contains(hashValue))
			return true;
		else
			textHashValueSet.add(hashValue);
		return false;
	}
	public static boolean beginIndex(){
		try {
			pageIndexer = new PageIndexer(Constants.indexDir);
			return true;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}
	public static boolean closeIndex(){
		if(pageIndexer != null)
			try {
				pageIndexer.close();
			} catch (IOException e) {
				e.printStackTrace();
				return false;
			}
		return true;
	}
}
